package Map;

/**
 * Stores an array of nodes which are the logical storage structure of the mindmap.
 * Provides methods for map wide (all nodes) operations.
 *
 * @author Lewis Marhin and John Gray-Spence
 */
public class Mindmap {

    private Node[] nodeList = new Node[Constants.MAX_NODES];

    /**
     * Constructor for the MindmapModel class.
     *
     * @param rootString Text that appears on the root node.
     * @param x x coordinate for the root node.
     * @param y y coordinate for the root node.
     */
    public Mindmap(String rootString, int x, int y) {   // DEFAULT
        nodeList[0] = new Node(rootString, x, y);
    }

    /**
     * Returns the list of nodes including the root.
     *
     * @return List of nodes.
     */
    public Node[] getNodeList() {
        return nodeList;
    }

    public void setNodeList(Node[] n) {
        nodeList = n;
    }

    /**
     * Returns number of nodes including root.
     *
     * @return Number of nodes.
     */
    public int getNodeCount() {
        return Node.nodeCount;
    }

    /**
     * Returns the root node which is always at [0].
     *
     * @return Root node.
     */
    public Node getRoot() {
        return nodeList[0];
    }

    /**
     * Creates a new node connected to a parent node.
     *
     * @param c Parent node to connect to.
     * @return Itself.
     */
    public Node newNode(Node c) {
        int i;
        int j = 0;

        for (i = 0; i < getNodeCount() + 1; i++) {
            if (nodeList[i] == null) {
                nodeList[i] = new Node("Node " + Integer.toString(i), c.getXloc(),
                        c.getYloc(), c);

                j = i;
                i = getNodeCount();
            }
        }
        return nodeList[j];
    }

    /**
     * Searches through an array searching for a nodes parent.
     *
     * @param i Array index of the node in question.
     * @return Array index of the parent node or root's constant ID
     */
    public int getParent(int i) {
        for (int j = 0; j < nodeList.length; j++) {
            if (nodeList[i].getParent() == nodeList[j]) {
                return j;
            }
        }
        return Constants.ROOT;
    }
}
